<?php

namespace app\admin\controller;

class Tag extends Common {

    public function index() {
        $tag_model = new \app\common\model\Tag();
        $list      = $tag_model->paginate(20);
        $this->assign('list', $list);
        return $this->fetch();
    }

    // 添加标签
    public function add() {
        $tag_model = new \app\common\model\Tag();

        if (request()->isPost()) {
            $input           = input('post.');
            $input['tag_id'] = null;
            $ret             = $tag_model->store($input);
            if ($ret['valid']) {
                $this->success($ret['msg'], 'index');
                exit;
            } else {
                $this->error($ret['msg'], null, '', 1);
                exit;
            }
        }
        $this->assign('tagData', null);
        return $this->fetch();
    }

    // 修改标签
    public function edit() {
        $tag_model = new \app\common\model\Tag();
        $tag_id    = input('param.tag_id');

        if (request()->isPost()) {
            $input           = input('post.');
            $input['tag_id'] = $tag_id;
            $ret             = $tag_model->store($input);
            if ($ret['valid']) {
                $this->success($ret['msg'], 'index');
                exit;
            } else {
                $this->error($ret['msg'], null, '', 1);
                exit;
            }
        }

        $tagData = $tag_model->find($tag_id);
        $this->assign('tagData', $tagData);
        return $this->fetch('add');
    }

    // 删除标签
    public function del() {
        $tag_id = input('get.tag_id');

        if (\app\common\model\Tag::destroy($tag_id)){
            $this->success('操作成功!','index','',1);
            exit;
        }else{
            $this->error('操作失败!',null,'',1);
            exit;
        }
    }
}
